﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using NLBS.Data;
using NLBS.Entity;
using NLBS.Data.Business;

namespace NLBS
{
    /// <summary>
    /// Represents an instance of a blog.  This was formerly known as the BlogConfig class. 
    /// We are attempting to distinguish this from settings stored in web.config. This class 
    /// is persisted via a <see cref="ObjectProvider"/>.
    /// </summary>
    [Serializable]
    public class BlogInfo
    {
        private static BlogInfo info;
        const int DefaultRecentCommentsLength = 50;
        private static Dictionary<string, Setting> setting;
        /// <summary>
        /// Gets the blog by id.
        /// </summary>
        /// <param name="blogId">Blog id.</param>
        /// <returns></returns>
        public static BlogInfo Instance()
        {
            ContentCache cache = ContentCache.Instantiate();
            info = (BlogInfo)cache["nlbsBlogInfo"];
            if (info == null)
            {
                info = new BlogInfo();
                setting = Cacher.GetSetting(CacheDuration.Long);
                cache.Insert("nlbsBlogInfo", info);
                
            }
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                info.CurrentUser = DbProvider.Instance().GetUserByName(HttpContext.Current.User.Identity.Name);
            } else
            {
                info.CurrentUser = CreateDefaultUser();
            }
                //info.CurrentUser = CreateDefaultUser();
                //if (HttpContext.Current.User != null)
                //{
                //    if (HttpContext.Current.User.Identity.IsAuthenticated)
                //    {
                //        if (info.CurrentUser.GroupID == 2 || info.CurrentUser == null)
                //            info.CurrentUser = DbProvider.Instance().GetUserByName(HttpContext.Current.User.Identity.Name);
                //    }
                //    else
                //    {
                //        info.CurrentUser = CreateDefaultUser();
                //    }
                //}
               
            //}
            
            //string key = string.Format("nlbsUser:{0}", info.CurrentUser.UserID);
            //cache.Insert(key, info.CurrentUser, CacheDuration.Long);
            return info;
        }

        private BlogInfo() { }

        public Setting GetSetting(string name)
        {
            return setting[name];
        }

        private static User CreateDefaultUser()
        {
            User defaultUser = new User();
            defaultUser.UserName = "游客";
            defaultUser.GroupID = 2;
            defaultUser.Group = UserGroupData.GetUserGroupByID(2);
            defaultUser.LastVisit = DateTime.Now;
            return defaultUser;
        }

        private User _currentUser;
        public User CurrentUser
        {
            get
            {
                return _currentUser;
            }
            set
            {
                _currentUser = value;
            }
        }

        public string Version
        {
            get { return "1.0.250"; }
            
        }
        
        public int EnableAjax
        {
            get { return setting["enableAjax"].Value0; }
            set { setting["enableAjax"].Value0 = value; }
        }

        public int EnableSecurityCode
        {
            get { return setting["enableSecurityCode"].Value0; }
            set { setting["enableSecurityCode"].Value0 = value; }
        }

        /// <summary>
        /// Gets or sets the title of the blog.
        /// </summary>
        /// <value></value>
        public string AnnounceDate
        {
            get { return setting["announceDate"].Value1; }
            set { setting["announceDate"].Value1 = value; }
        }

        /// <summary>
        /// Gets or sets the Announce of the blog.
        /// </summary>
        /// <value></value>
        public string Announce
        {
            get { return setting["announce"].Value1; }
            set { setting["announce"].Value1 = value; }
        }

        /// <summary>
        /// Gets or sets the AnnounceShow of the blog.
        /// </summary>
        /// <value></value>
        public int AnnounceShow
        {
            get { return setting["announceShow"].Value0; }
            set { setting["announceShow"].Value0 = value; }
        }

        /// <summary>
        /// Gets or sets the announceUBBFlags of the blog.
        /// </summary>
        /// <value></value>
        public string announceUBBFlags
        {
            get { return setting["announceUBBFlags"].Value1; }
            set { setting["announceUBBFlags"].Value1 = value; }
        }


        /// <summary>
        /// Gets or sets the Link of the blog.
        /// </summary>
        /// <value></value>
        public string Link
        {
            get { return setting["links"].Value1; }
            set { setting["links"].Value1 = value; }
        }

        /// <summary>
        /// Gets or sets the title of the blog.
        /// </summary>
        /// <value></value>
        public string Title
        {
            get { return setting["blogTitle"].Value1; }
            set { setting["blogTitle"].Value1 = value; }
        }

        /// <summary>
        /// Gets or sets the sub title of the blog.
        /// </summary>
        /// <value></value>
        public string SubTitle
        {
            get { return setting["blogDescription"].Value1; }
            set { setting["blogDescription"].Value1 = value; }
        }

        /// <summary>
        /// Gets or sets the BaseUrl of the blog.
        /// </summary>
        /// <value></value>
        public string BaseUrl
        {
            get { return setting["baseURL"].Value1; }
            set { setting["baseURL"].Value1 = value; }
        }


        ///// <summary>
        ///// Gets the virtual URL for the site with preceding and trailing slash.  For example, "/" or "/Subtext.Web/" or "/Blog/".
        ///// </summary>
        ///// <value>The virtual URL.</value>
        //public string VirtualUrl
        //{
        //    get
        //    {
        //        if (this.virtualUrl == null)
        //        {
        //            this.virtualUrl = "/";
        //            string appPath = HttpContext.Current.Request.ApplicationPath;
        //            if (appPath.Length > 0)
        //            {
        //                this.virtualUrl += appPath + "/";
        //            }

        //        }
        //        return this.virtualUrl;
        //    }
        //}
       

        /// <summary>
        /// Gets or sets the path (url) to the image directory.
        /// </summary>
        /// <value></value>
        public string LogoImage
        {
            get { return setting["logoImage"].Value1; }
            set { setting["logoImage"].Value1 = value; }
        }

        /// <summary>
        /// Gets or sets the user name for the owner of the blog.
        /// </summary>
        /// <value></value>
        public string BlogWebMaster
        {
            get { return setting["blogWebMaster"].Value1; }
            set { setting["blogWebMaster"].Value1 = value; }
        }

        /// <summary>
        /// Gets or sets the email of the blog owner.
        /// </summary>
        /// <value></value>
        public string Email
        {
            get { return setting["blogWebMasterEmail"].Value1; }
            set { setting["blogWebMasterEmail"].Value1 = value; }
        }

        /// <summary>
        /// Gets or sets the language the blog is in..
        /// </summary>
        /// <value></value>
        public string Language
        {
            get { return setting["blogLanguage"].Value1; }
            set { setting["blogLanguage"].Value1 = value; }
        }

        /// <summary>
        /// Gets or sets the date that the blog's configuration 
        /// was last updated.
        /// </summary>
        /// <value></value>
        //public string LastUpdated
        //{
        //    get { return setting["lastUpdated"].Value1; }
        //    set { setting["lastUpdated"].Value1 = value; }
        //}

        private int _itemCount = 15;
        /// <summary>
        /// Gets or sets the count of posts displayed on the front page 
        /// of the blog.
        /// </summary>
        /// <value></value>
        public int ItemCount
        {
            get { return _itemCount; }
            set { _itemCount = value; }
        }

        private int _categoryListPostCount = 10;
        /// <summary>
        /// Gets or sets the count of posts displayed on the category pages. 
        /// </summary>
        /// <value></value>
        public int CategoryListPostCount
        {
            get { return _categoryListPostCount; }
            set
            {
                if (value < 0)
                {
                    value = 0;//needed when upgrading from versions that did not have this column ("CategoryListPostCount") in the subtext_Config table.
                }
                _categoryListPostCount = value;
            }
        }

        private int _storyCount;
        /// <summary>
        /// Gets or sets the story count.
        /// </summary>
        /// <value></value>
        public int StoryCount
        {
            get { return this._storyCount; }
            set { this._storyCount = value; }
        }



        /// <summary>
        /// The port the blog is listening on.
        /// </summary>
        public int Port
        {
            get
            {
                if (HttpContext.Current != null)
                {
                    return HttpContext.Current.Request.Url.Port;
                }
                return 80;
            }
        }

 

        /// <summary>
        /// Gets or sets the days till comments close on a post.  
        /// The count starts when a post is created.
        /// </summary>
        /// <value></value>
        public int DaysTillCommentsClose
        {
            get { return _daysTillCommentsClose; }
            set { _daysTillCommentsClose = value; }
        }

        int _daysTillCommentsClose = int.MaxValue;

        /// <summary>
        /// Gets or sets the delay in minutes, between any two successive comments from 
        /// the same IP address.  This helps prevents comment bombing attacks.
        /// </summary>
        /// <value></value>
        public int CommentDelayInMinutes
        {
            get
            {
                if (_commentDelayInMinutes < 0 || _commentDelayInMinutes == int.MaxValue)
                    return 0;
                else
                    return _commentDelayInMinutes;
            }
            set { _commentDelayInMinutes = value; }
        }

        int _commentDelayInMinutes;

        /// <summary>
        /// Gets or sets the number of recent comments to display in 
        /// the RecentComments control.
        /// </summary>
        /// <value></value>
        public int NumberOfRecentComments
        {
            get
            {
                if (_numberOfRecentComments < 0 || _numberOfRecentComments == int.MaxValue)
                    return 0;
                else
                    return _numberOfRecentComments;
            }
            set { _numberOfRecentComments = value; }
        }

        int _numberOfRecentComments;

        /// <summary>
        /// Gets or sets the number of characters to use to display recent comments  
        /// in the RecentComments control.
        /// </summary>
        /// <value></value>
        public int RecentCommentsLength
        {
            get
            {
                if (_recentCommentsLength < 0 || _recentCommentsLength == int.MaxValue)
                    return DefaultRecentCommentsLength;
                else
                    return _recentCommentsLength;
            }
            set { _recentCommentsLength = value; }
        }

        int _recentCommentsLength;

        private string _skin;
        /// <summary>
        /// Gets or sets the <see cref="SkinConfig"/> instance 
        /// which contains information about the specified skin.
        /// </summary>
        /// <value></value>
        public string Skin
        {
            get { return _skin; }
            set { _skin = value; }
        }

        /// <summary>
        /// Gets a value indicating whether the blog has news. 
        /// News can be entered in the Admin section.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the blog has news; otherwise, <c>false</c>.
        /// </value>
        public bool HasNews
        {
            get { return News != null && News.Trim().Length > 0; }
        }

        private string news;
        /// <summary>
        /// Gets or sets the news.
        /// </summary>
        /// <value></value>
        public string News
        {
            get { return news; }
            set { news = value; }
        }

        private string _author = "Subtext Weblog";
        /// <summary>
        /// Gets or sets the author of the blog.
        /// </summary>
        /// <value></value>
        public string Author
        {
            get { return _author; }
            set { _author = value; }
        }

        /// <summary>
        /// Gets or sets the license URL.  This is used to 
        /// Used to specify a license within a syndicated feed. 
        /// Does not have to be a creative commons license. 
        /// <see href="http://backend.userland.com/creativeCommonsRssModule" />
        /// </summary>
        /// <value></value>
        public string LicenseUrl
        {
            get { return _licenseUrl; }
            set { _licenseUrl = value; }
        }

        string _licenseUrl;

        /// <summary>
        /// Gets a value indicating whether [feed burner enabled].
        /// </summary>
        /// <value><c>true</c> if [feed burner enabled]; otherwise, <c>false</c>.</value>
        public bool FeedBurnerEnabled
        {
            get
            {
                return !String.IsNullOrEmpty(this.feedBurnerName);
            }
        }

        /// <summary>
        /// Gets or sets the name of the feedburner account. 
        /// This is the portion of the feedburner URL after:
        /// http://feeds.feedburner.com/
        /// </summary>
        /// <value>The name of the feed burner.</value>
        public string FeedBurnerName
        {
            get { return this.feedBurnerName; }
            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    if (value.Contains("/") || value.Contains("\\"))
                        throw new InvalidOperationException("Cannot specify a feedburner name with invalid characters.");
                }
                this.feedBurnerName = value;
            }
        }

        string feedBurnerName;

        ///// <summary>
        ///// Gets the root URL for this blog.  For example, "http://example.com/" or "http://example.com/blog/".
        ///// </summary>
        ///// <value></value>
        //public Uri RootUrl
        //{
        //    get
        //    {
        //        if (this.rootUrl == null)
        //        {
        //            this.rootUrl = HostFullyQualifiedUrl;
        //            if (this.Subfolder != null && this.Subfolder.Length > 0)
        //            {
        //                this.rootUrl = new Uri(this.rootUrl, this.Subfolder + "/");
        //            }
        //        }
        //        return this.rootUrl;
        //    }
        //}
        //Uri rootUrl = null;

 

        /// <summary>
        /// Gets the virtual directory/application root for the site.  
        /// This is really just a formatted version of the 
        /// HttpContext.Current.Request.ApplicationPath property that always ends with a slash.
        /// </summary>
        /// <value>The virtual URL.</value>
        public string VirtualDirectoryRoot
        {
            get
            {
                string virtualDirectory = HttpContext.Current.Request.ApplicationPath;
                if (virtualDirectory.Length == 0)
                {
                    return "/";
                }
                if (!virtualDirectory.EndsWith("/"))
                {
                    virtualDirectory += "/";
                }

                if (!virtualDirectory.StartsWith("/"))
                {
                    virtualDirectory = "/" + virtualDirectory;
                }
                return virtualDirectory;
            }
        }

        #region Counts

        /// <summary>
        /// Gets or sets the total number of posts.
        /// </summary>
        /// <value></value>
        public int ArticleCount
        {
            get { return setting["counterArticle"].Value0; }
            set { setting["counterArticle"].Value0 = value; }
        }
        /// <summary>
        /// Gets or sets the comment count.
        /// </summary>
        /// <value></value>
        public int CommentCount
        {
            get { return setting["counterComment"].Value0; }
            set { setting["counterComment"].Value0 = value; }
        }
        
        /// <summary>
        /// Gets or sets the ping track count.
        /// </summary>
        /// <value></value>
        public int PingTrackCount
        {
            get { return setting["counterTrackback"].Value0; }
            set { setting["counterTrackback"].Value0 = value; }
        }

        /// <summary>
        /// Gets or sets the ping track count.
        /// </summary>
        /// <value></value>
        public int UserCount
        {
            get { return setting["counterUser"].Value0; }
            set { setting["counterUser"].Value0 = value; }
        }

        /// <summary>
        /// Gets or sets the ping track count.
        /// </summary>
        /// <value></value>
        public int VisitorCount
        {
            get { return setting["counterVisitor"].Value0; }
            set { setting["counterVisitor"].Value0 = value; }
        }

        #endregion

        #region article
        public int DefaultViewMode
        {
            get { return setting["defaultViewMode"].Value0; }
            set { setting["defaultViewMode"].Value0 = value; }
        }

        public int ArticlePerPageNormal
        {
            get { return setting["articlePerPageNormal"].Value0; }
            set { setting["articlePerPageNormal"].Value0 = value; }
        }
        public int ArticlePerPageList
        {
            get { return setting["articlePerPageList"].Value0; }
            set { setting["articlePerPageList"].Value0 = value; }
        }

        public int RecentArticleList
        {
            get { return setting["recentArticleList"].Value0; }
            set { setting["recentArticleList"].Value0 = value; }
        }

        public int ContentAutoSplitChars
        {
            get { return setting["contentAutoSplitChars"].Value0; }
            set { setting["contentAutoSplitChars"].Value0 = value; }
        }

        public int EnableContentAutoSplit
        {
            get { return setting["enableContentAutoSplit"].Value0; }
            set { setting["enableContentAutoSplit"].Value0 = value; }
        }

        #endregion

        #region comment
        //
        public int EnableRegister
        {
            get { return setting["enableRegister"].Value0; }
            set { setting["enableRegister"].Value0 = value; }
        }

        public int EnableComment
        {
            get { return setting["enableComment"].Value0; }
            set { setting["enableComment"].Value0 = value; }
        }

        public int MaxCommentLength
        {
            get { return setting["maxCommentLength"].Value0; }
            set { setting["maxCommentLength"].Value0 = value; }
        }

        public int EnableTrackbackOut
        {
            get { return setting["enableTrackbackOut"].Value0; }
            set { setting["enableTrackbackOut"].Value0 = value; }
        }


        public int EnableTrackbackIn
        {
            get { return setting["enableTrackbackIn"].Value0; }
            set { setting["enableTrackbackIn"].Value0 = value; }
        }


        public int MaxCommentURLCount
        {
            get { return setting["maxCommentURLCount"].Value0; }
            set { setting["maxCommentURLCount"].Value0 = value; }
        }

    
        public int RecentCommentList
        {
            get { return setting["recentCommentList"].Value0; }
            set { setting["recentCommentList"].Value0 = value; }
        }


       #endregion

        #region VisitorRecord
        private int _enableVisitorRecord;

        public int EnableVisitorRecord
        {
            get { return this._enableVisitorRecord; }
            set { this._enableVisitorRecord = value; }
        }

        private int _maxVisitorRecord;

        public int MaxVisitorRecord
        {
            get { return this._maxVisitorRecord; }
            set { this._maxVisitorRecord = value; }
        }

        #endregion
       
        
        public int MinPostDuration
        {
            get { return setting["minPostDuration"].Value0; }
            set { setting["minPostDuration"].Value0 = value; }
        }

        //showTrackbackPosition

        //showTrackbackWithComment
        public int EnableGuestBook
        {
            get { return setting["enableGuestBook"].Value0; }
            set { setting["enableGuestBook"].Value0 = value; }
        }

        public int EntryPerPageGuestBook
        {
            get { return setting["entryPerPageGuestBook"].Value0; }
            set { setting["entryPerPageGuestBook"].Value0 = value; }
        }

        public int ListEntryPerPage
        {
            get { return setting["listEntryPerPage"].Value0; }
            set { setting["listEntryPerPage"].Value0 = value; }
        }

      

        #region path

          
        public string SkinPath
        {
            get { return setting["styleSheet"].Value1; }
            set { setting["styleSheet"].Value1 = value; }
        }

        /// <summary>
        /// imageFolder
        /// </summary>
        /// <value></value>
        public string ImagePath
        {
            get { return setting["imageFolder"].Value1; }
            set { setting["imageFolder"].Value1 = value; }
        }

        /// <summary>
        /// smiliesFolder
        /// </summary>
        /// <value></value>
        public string SmiliePath
        {
            
            get { 
                string path=setting["smiliesFolder"].Value1;
                if (!path.EndsWith("/"))
                {
                    path += "/";
                }
                if (!path.StartsWith("/"))
                {
                    path = "/"+path;
                }
                return path;
            }
            set { setting["smiliesFolder"].Value1 = value; }
        }

        #endregion

        private int _smiliesPerRow;
        public int SmiliesPerRow
        {
            get { return this._smiliesPerRow; }
            set { this._smiliesPerRow = value; }
        }

        #region upload

       
        public int EnableUpLoad
        {
            get { return setting["enableUpload"].Value0; }
            set { setting["enableUpload"].Value0 = value; }
        }

        
        public int UpLoadSize
        {
            get { return setting["uploadSize"].Value0; }
            set { setting["uploadSize"].Value0 = value; }
        }

       
        public string UploadPath
        {
            get { return setting["uploadPath"].Value1; }
            set { setting["uploadPath"].Value1 = value; }
        }

        
        public string UploadType
        {
            get { return setting["uploadTypes"].Value1; }
            set { setting["uploadTypes"].Value1 = value; }
        }

        #endregion
    }
}
